package com.woyowodaows.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.woyowodaows.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/4.
 */

public class SingleListDialog extends Dialog{

    @BindView(R.id.tv_single_dialog_title)
    TextView title;
    @BindView(R.id.lv_single_dialog)
    ListView lv;
    private String[] items ;
    private Context context;
    private String t;
    private OnItemClickListener itemClickListener;

    public SingleListDialog(@NonNull Context context, String[] items) {
        super(context, R.style.MyDialogStyle);
        this.items = items;
        this.context =context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_single_list,null);
        setContentView(view);
        ButterKnife.bind(this,view);
        title.setText(t);

        initListView();

        setCanceledOnTouchOutside(true);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        lp.width = (int) (d.widthPixels*0.8);
        dialogWindow.setAttributes(lp);

    }

    public void  setTitle(String t){
        this.t = t;
    }

    private void initListView(){
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        ArrayAdapter adapter = new ArrayAdapter<String>(context,R.layout.dialog_single_dialog_item,R.id.tv_single_item,items);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (itemClickListener != null){
                    itemClickListener.ItemClickListener(i);
                }

            }
        });


    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.itemClickListener = onItemClickListener;

    }


    public interface  OnItemClickListener{
        void ItemClickListener(int i);
    }







}
